* Author: Anina Schwarzenbach
* generated April 2015
* last review August 2020

clear
*cd: 
set more off
*load ADOS
sysdir set PERSONAL "../../../ados"

***************** GENERATE COMMON DATASET
clear
use "../../STATAfiles/S11DE_version4scales.dta"
keep ///
	age1 age2 age3 age4 attmtr1 attmtr2 attmtr3 attmtr4 attmtr5 attmtr6 attsch1 attsch2 attsch3	attviol1 attviol2 attviol4 attviol5	citizen	city country ///
	dole_m	dole_p dole1 dole2 drunk educ1 educ2	///
	ethnic1 ethnic2 ethnic3 ethnic4 ethnic5 ethn1DE ethn2DE ethn3DE ethn4DEa ethn4DEb ethn4DEc ///
	ethngpDE_1 ethngpDE_2 ethngpDE_3 ethngpDE_4 ethngpDE_5 ethngpDE_6 ethngpDE_7 ethngpDE_miss ///	ethngpFR_1 ethngpFR_2 ethngpFR_3 ethngpFR_4 ethngpFR_5 ethngpFR_6 ethngpFR_7 ethngpFR_miss ///
	ethnkidDE_1 ethnkidDE_2 ethnkidDE_3 ethnkidDE_4 ethnkidDE_5 ethnkidDE_6 ethnkidDE_7 ethnkidDE_miss  ///
	ethnparDE_1 ethnparDE_2 ethnparDE_3 ethnparDE_4 ethnparDE_5 ethnparDE_6 ethnparDE_7 ethnparDE_miss  ///
	famsetd generat1 generat2 generat3 generat1DE generat2DE generat3DE generat4DEa generat4DEb ///
	intermmt interplc interyth ///
	kid	lastcont1	lastcont2	lastcont3 mbfriend mbfriendc	///
	natident1	natident2	nbhd1	nbhd2	nbhd3	nbhd4	nbhd5	nbhd6 nights1 nights2 ///	
	pact1 pact2 pact3 pact4 pact5 pact6 pact7 ///
	parmon1	parmon2	pbeh1	pbeh2	pbeh3	pbeh4	///	
	pcont_d	pcont_k	pcont_k2 pcont_sum	pcont1	pcont1d	pcont1r	pcont2 pcont2d	pcont2r	pcont3 pcont3d pcont3r	///	
	pcont4	pcont4_k pcont4d pcont4r pcont5	pcont5d	pcont5r	pcont6 pcont6d pcont6r pcont7 pcont7d pcont7r pcont8 pcont8d pcont8r ///	
	pconta_k pconta_k2 pconta_sum pcontad pcontb_k	pcontb_k2 pcontb_sum pcontbd pcontc_k pcontc_k2	pcontc_sum pcontcd ///	
	peerdel_k peerdel_sum  peerdel2 peerdel5 peerdel6	peerdel7 ///	
	pleg1	pleg2	pleg3	pleg4	pleg5	pleg6	pleg7	pleg8	///	
	relig1	relig2	schlid	sex	srd_k	srd_k2	srd_sum	srd1 srd2 srd3 srd4 srd5 srd6	srd7 srdmiss status	street	///	
	vic_k vic_k2 vic_sum vic1 vic2 vic3	vic4 vicexp1 vicexp2 vicmiss	///	
	xmod ybeh1 ybeh2 ybeh3 ybeh4 ybeh5 ybeh6 ///
	zage1 znatident1 znatident2 zpcont_sum zpconta_sum zpcontb_sum zpcontc_sum zpeerdel_sum zspeerdel zspeerdelDE  ///
	sattsch sattviol snbhd1 snbhd2 sparmon spbeh spbehr spleg splegr spleg_fair splegr_fair zspleg_fair spleg_obey splegr_obey zspleg_obey srd_sum svicexp zsvicexp svicexpr vic_sum ///	
	zsattsch zsattviol zsnbhd1 zsnbhd2 zsparmon zspbeh zspleg zsrd_sum zvic_sum ///	
	sactiv1DE zsactiv1DE  ///
	sattmtrDE zsattmtrDE sattschDE zsattschDE sattviolDE zsattviolDE sparmonDE zsparmonDE ///
	szenario1 szenario2 szenario3 szenario1r szenario2r szenario3r ///
	attsch1f attsch2f attsch3f attsch4f attviol1f attviol2 attviol4 attviol5f pleg2f pleg5f pleg6f pleg7f pbeh1f pbeh2f ///
	srd_k2DE ISEIhigh zISEIhigh vic_k2 ///
	d6_3f d7_4f peerdel1 peerdel2 peerdel3 peerdel4 peerdel5 peerdel6 peerdel7 peerdel8 peerdel9 ///
	xtyp xwn xids xtypdet xtypw


append using "../../STATAfiles/S11FR_version4scales.dta"

* DISTRICT DATA
*fre x1_2
*fre INSEEcor
*fre INSEEetab commune COMMUNEcor insee INSEEcor


/*string  XGEOID (A12).

egen xgeoid=concat (xcitydefr) if xcitydefr==3

do if (xcity = 3).

gen xgeoid = concat("C",ltrim(string(xstnrn,F4.0))).
else if (xcity = 4).
gen xgeoid = lpad(ltrim(string(xstnrn,F4.0)),3,"0").
gen xgeoid = concat("M",xgeoid).  
end if.

summarize xgeoid

select if (xtyp ne 8).          
EXECUTE.
*/


* ADD
**** SCALES 
**** GENERATION VARIABLES FOR JOINT DATASET MISSING

keep ///
	age1 age2 age3 age4 attmtr1 attmtr2 attmtr3 attmtr4 attmtr5 attmtr6 attsch1 attsch2 attsch3	attviol1 attviol2 attviol4 attviol5	citizen	city country ///
	dole_m	dole_p dole1 dole2 drunk educ1 educ2	///
	ethnic1 ethnic2 ethnic3 ethnic4 ethnic5 ethn1DE ethn2DE ethn3DE ethn4DEa ethn4DEb ethn4DEc ///
	ethngpDE_1 ethngpDE_2 ethngpDE_3 ethngpDE_4 ethngpDE_5 ethngpDE_6 ethngpDE_7 ethngpDE_miss ///	
	ethngpFR_1 ethngpFR_2 ethngpFR_3 ethngpFR_4 ethngpFR_5 ethngpFR_6 ethngpFR_7 ethngpFR_miss ///
	ethnkidDE_1 ethnkidDE_2 ethnkidDE_3 ethnkidDE_4 ethnkidDE_5 ethnkidDE_6 ethnkidDE_7 ethnkidDE_miss  ///
	ethnparDE_1 ethnparDE_2 ethnparDE_3 ethnparDE_4 ethnparDE_5 ethnparDE_6 ethnparDE_7 ethnparDE_miss  ///
	famsetd generat1 generat2 generat3 generat1DE generat2DE generat3DE generat4DEa generat4DEb ///
	intermmt interplc interyth ///
	kid	lastcont1	lastcont2	lastcont3 mbfriend mbfriendc	///
	natident1	natident2	nbhd1	nbhd2	nbhd3	nbhd4	nbhd5	nbhd6 nights1 nights2 ///	
	pact1 pact2 pact3 pact4 pact5 pact6 pact7 ///
	parmon1	parmon2	pbeh1	pbeh2	pbeh3	pbeh4	///	
	pcont_d	pcont_k	pcont_k2 pcont_sum	pcont1	pcont1d	pcont1r	pcont2 pcont2d	pcont2r	pcont3 pcont3d pcont3r	///	
	pcont4	pcont4_k pcont4d pcont4r pcont5	pcont5d	pcont5r	pcont6 pcont6d pcont6r pcont7 pcont7d pcont7r pcont8 pcont8d pcont8r ///	
	pconta_k pconta_k2 pconta_sum pcontad pcontb_k	pcontb_k2 pcontb_sum pcontbd pcontc_k pcontc_k2	pcontc_sum pcontcd ///	
	peerdel_k peerdel_sum  peerdel2 peerdel5 peerdel6	peerdel7 ///	
	pleg1	pleg2	pleg3	pleg4	pleg5	pleg6	pleg7	pleg8	///	
	relig1	relig2	schlid	sex	srd_k	srd_k2	srd_sum	srd1 srd2 srd3 srd4 srd5 srd6	srd7 srdmiss status	street	///	
	vic_k vic_k2 vic_sum vic1 vic2 vic3	vic4 vicexp1 vicexp2 vicmiss	///	
	xmod ybeh1 ybeh2 ybeh3 ybeh4 ybeh5 ybeh6 ///
	zage1 znatident1 znatident2 zpcont_sum zpconta_sum zpcontb_sum zpcontc_sum zpeerdel_sum zspeerdel zspeerdelDE ///
	sattsch sattviol snbhd1 snbhd2 sparmon spbeh spbehr spleg splegr spleg_fair splegr_fair zspleg_fair spleg_obey splegr_obey zspleg_obey srd_sum svicexp zsvicexp svicexpr vic_sum ///	
	zsattsch zsattviol zsnbhd1 zsnbhd2 zsparmon zspbeh zspleg zsrd_sum  zvic_sum ///	
	sactiv1DE zsactiv1DE   ///
	sattmtrDE zsattmtrDE sattschDE zsattschDE sattviolDE zsattviolDE sparmonDE zsparmonDE ///
	szenario1 szenario2 szenario3 szenario1r szenario2r szenario3r ///
	attsch1f attsch2f attsch3f attsch4f attviol1f attviol2 attviol4 attviol5f pleg2f pleg5f pleg6f pleg7f pbeh1f pbeh2f ///
	srd_k2DE ISEIhigh zISEIhigh vic_k2 ///
	d6_3f d7_4f peerdel1 peerdel2 peerdel3 peerdel4 peerdel5 peerdel6 peerdel7 peerdel8 peerdel9 ///
	xtyp xwn xids xtypdet xtypw TypEtab6 weight


saveold "../../STATAfiles/S11DEFR_version4.dta", replace 
compress


*** CREATE DATASET WITH GEOFILES ********

use "../../STATAfiles/geofiles/S11DEFR_XGEODATA", clear
sort xgeoid 
drop xcity country
save "../../STATAfiles/geofiles/S11DEFR_XGEODATAnew", replace

use "../../STATAfiles/geofiles/S11DEFR_KID_GEOIDs", clear
replace xgeoid="" if xgeoid=="C." | xgeoid=="M." | xgeoid=="G" | xgeoid=="L"
drop XGEOID2
drop if xgeoid==""
save "../../STATAfiles/geofiles/S11DEFR_KID_GEOIDsnew", replace


use "../../STATAfiles/geofiles/S11DEFR_KID_GEOIDsnew", clear
sort xgeoid 

merge m:1 xgeoid using "../../STATAfiles/geofiles/S11DEFR_XGEODATAnew"
sort kid
drop _merge
drop if kid==.

save "../../STATAfiles/geofiles/S11DEFR_GEO", replace

use  "../../STATAfiles/S11DEFR_version4"
sort kid

merge 1:1 kid using "../../STATAfiles/geofiles/S11DEFR_GEO"
saveold "../../STATAfiles/S11DEFR_version4GEO", replace


* CORRECTION DATASET
******************************

use "../../STATAfiles/S11DEFR_version4GEO.dta", clear
drop _merge
sort xgeoid
 
merge m:1 xgeoid using "../../STATAfiles/geofiles/S11DEFR_L2N15_GEODAT_MERGE.dta"
sort kid
drop _merge
drop if kid==.
 
saveold "../../STATAfiles/S11DEFR_v5_L2N15.dta", replace
 







